#!/bin/sh
# Test a command for presence and ability to output the sha1 hash of a file.
check_hashcmd() {
        which $1 &&
        $1 </dev/null 2>/dev/null |
        awk '{if ($1 != "da39a3ee5e6b4b0d3255bfef95601890afd80709") { exit 1; }}'
}

check_voice_folder() {
        voice_file=${1#mb-}
        if [ -f "/usr/share/mbrola/$voice_file" ]; then
                voice_file="/usr/share/mbrola/$voice_file"
        elif [ -f "/usr/share/mbrola/$voice_file/$voice_file" ]; then
                voice_file="/usr/share/mbrola/$voice_file/$voice_file"
        elif [ -f "/usr/share/mbrola/voices/$voice_file" ]; then
                voice_file="/usr/share/mbrola/voices/$voice_file"
        else
                voice_file=""
        fi
}

test_voice () {
        MBVOICE=$1
        EXPECTED=$2
        TEST_TEXT=$3
        check_voice_folder $MBVOICE
        if [ "$voice_file" != "" ]; then
                echo "testing ${MBVOICE} ${TEST_TEXT} ... "
                ESPEAK_DATA_PATH=`pwd` LD_LIBRARY_PATH=src:${LD_LIBRARY_PATH} \
                src/espeak-ng --stdout -v ${MBVOICE} "${TEST_TEXT}" | \
                $sha1sum | awk '{ print $1 }' > actual.txt
                echo "${EXPECTED}" > expected.txt
                diff expected.txt actual.txt || exit 1
        else
                echo "$MBVOICE was not tested"
        fi
}

# Test some common commands to find the correct one for the system being tested on.
echo -n "checking for SHA1 hashing command ... "
if check_hashcmd sha1sum; then
        sha1sum=sha1sum
elif check_hashcmd sha1; then
        sha1sum=sha1
elif check_hashcmd shasum; then
        sha1sum=shasum
else
        echo "no"
        exit 1
fi


echo -n "checking if MBROLA is installed ... "
if [ "`which mbrola`" != "" ]; then
        echo "yes"
else
        echo "no"
        exit
fi

test_voice mb-fr4 31fae066f45d4a9dc56289344f28dd00bce77875 "Bonjour"


